package com.ruoyi.app.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.CharacterUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MybatisUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.ShopCustomerMapper;
import com.ruoyi.app.domain.ShopCustomer;
import com.ruoyi.app.service.IShopCustomerService;

/**
 * 客户信息Service业务层处理
 * 
 * @author zkq
 * @date 2024-07-28
 */
@Service
public class ShopCustomerServiceImpl extends ServiceImpl<ShopCustomerMapper, ShopCustomer> implements IShopCustomerService
{
    @Autowired
    private ShopCustomerMapper shopCustomerMapper;

    /**
     * 查询客户信息
     * 
     * @param id 客户信息主键
     * @return 客户信息
     */
    @Override
    public ShopCustomer selectShopCustomerById(String id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询客户信息列表
     * 
     * @param shopCustomer 客户信息
     * @return 客户信息
     */
    @Override
    public List<ShopCustomer> selectShopCustomerList(ShopCustomer shopCustomer)
    {
        LambdaQueryWrapper<ShopCustomer> lambdaQueryWrapper = new LambdaQueryWrapper<>(ShopCustomer.class);
        Map<String, Object> params = shopCustomer.getParams();
        Object beginTime = params.get("beginTime");
        Object endTime = params.get("endTime");
        lambdaQueryWrapper.between((beginTime!= null && endTime!= null), ShopCustomer::getCreateTime, beginTime, endTime);
        lambdaQueryWrapper.eq(ShopCustomer::getShopId, shopCustomer.getShopId());
        lambdaQueryWrapper.orderByAsc(ShopCustomer::getInitial);
        return baseMapper.selectList(lambdaQueryWrapper);
//        return MybatisUtils.list(baseMapper, shopCustomer);
    }

    @Override
    public boolean save(ShopCustomer entity) {
        if(StringUtils.isNotEmpty(entity.getName())){
            entity.setInitial(CharacterUtils.extractInitial(entity.getName()));
        }
        return super.save(entity);
    }

    @Override
    public boolean updateById(ShopCustomer entity) {
        if(StringUtils.isNotEmpty(entity.getName())){
            entity.setInitial(CharacterUtils.extractInitial(entity.getName()));
        }
        return super.updateById(entity);
    }

    /**
     * 添加或修改
     * @param shopCustomer
     * @return
     */
    @Override
    public boolean addOrUpdate(ShopCustomer shopCustomer) {
        if(org.apache.commons.lang3.StringUtils.isEmpty(shopCustomer.getId())){
            return save(shopCustomer);
        } else {
            return updateById(shopCustomer);
        }
    }
}
